#pragma once

#define _USE_MATH_DEFINES		// Math Constants
#include <cmath>
#include <cstdlib>

namespace RayTracer
{

	class CVector4
	{
	public:

		CVector4(void) : x(0.f), y(0.f), z(0.f), w(1.f) {};
		CVector4(float X, float Y, float Z, float W) : x(X), y(Y), z(Z), w(W) {};

		~CVector4(void) {};

		union
		{
			struct { float x, y, z, w; };
			struct { float r, g, b, a; };
		};

		void Set(float X, float Y, float Z, float W)
		{
			x = X;
			y = Y;
			z = Z;
			w = W;
		}

		void Normalize()
		{
			float l = 1.f / Length();
			x *= l;
			y *= l;
			z *= l;
			w *= l;
		}

		float Length()
		{
			float res = sqrt(x * x + y * y + z * z + w * w);
			return res;
		}

		float Length2()
		{
			float res = x * x + y * y + z * z + w * w;
			return res;
		}

		float Dot (const CVector4& v)
		{
			float res = x * v.x + y * v.y + z * v.z + w * v.w;
			return res;
		}

		void operator+= (const CVector4& v)
		{
			x += v.x;
			y += v.y;
			z += v.z;
			w += v.w;
		}

		void operator-= (const CVector4& v)
		{
			x -= v.x;
			y -= v.y;
			z -= v.z;
			w -= v.w;
		}

		void operator*= (const float& f)
		{
			x *= f;
			y *= f;
			z *= f;
			w *= f;
		}

		void operator*= (const CVector4& v)
		{
			x *= v.x;
			y *= v.y;
			z *= v.z;
			w *= v.w;
		}

		CVector4 operator- ()
		{
			return CVector4(-x, -y, -z, -w);
		}

		friend CVector4 operator+ (const CVector4& v1, const CVector4& v2)
		{
			return CVector4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
		}

		friend CVector4 operator- (const CVector4& v1, const CVector4& v2)
		{
			return CVector4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
		}
		
		friend CVector4 operator* (const CVector4& v1, const CVector4& v2)
		{
			return CVector4(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w);
		}

		friend CVector4 operator* (const CVector4& v, const float& f)
		{
			return CVector4(v.x * f, v.y * f, v.z * f, v.w * f);
		}

		friend CVector4 operator* (const float& f, const CVector4& v)
		{
			return CVector4(v.x * f, v.y * f, v.z * f, v.w * f);
		}

		friend CVector4 Normalize (const CVector4& v)
		{
			float l = 1.f / sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w + v.w);
			return v * l;
		}

		friend float Dot (const CVector4& v1, const CVector4& v2)
		{
			float res = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
			return res;
		}

		friend float Distance (const CVector4& v1, const CVector4& v2)
		{
			float X = v1.x - v2.x;
			float Y = v1.y - v2.y;
			float Z = v1.z - v2.z;
			float W = v1.w - v2.w;
			float res = sqrt(X * X + Y * Y + Z * Z + W * W);
			return res;
		}

		friend float Distance2 (const CVector4& v1, const CVector4& v2)
		{
			float X = v1.x - v2.x;
			float Y = v1.y - v2.y;
			float Z = v1.z - v2.z;
			float W = v1.w - v2.w;
			float res = X * X + Y * Y + Z * Z + W * W;
			return res;
		}
	};

}